<?php
/*
    genome.class
*/
require_once("random.class.php");
class genome {

    public $chromosome = array();
    public static $NBGENE = 9;
    public static $MAXLEN = 7;
    private $valid;
/* genome  */
    public function new_genome (array $c ) {
	$this->chromosome=$c;
	$valid = true;
	//var_dump($this->chromosome);
    }
    public function new_ran_genome (random $r) {
	$this->get_val($r);
	$this->valid = true;
    }
/* Isvalid  */    
    public function is_valid () {
	return $this->valid;
    }
/* mutate  */
    public function mutate (int $MaxSeverity,random $r) {
	if ( $MaxSeverity > 0 ) {
	    $p = $r->next_int() % ( self::$NBGENE );
	    $p = ($p < 0) ? - $p : $p;
	    $severity = $r->next_int();
	    $mute = $severity % $MaxSeverity + 1;
	    $this->chromosome[$p] += $mute;
	    if ($p < (self::$NBGENE - 1)) {
		$this->chromosome[$p] %= (self::$MAXLEN);	
	    }else{
		$this->chromosome[$p] %= (self::$MAXLEN + 1);	
	    }
	}	
    }
/* GetVal */
    public function get_val (random $r) {
	for ($i=0;$i < self::$NBGENE - 1;$i++) {
	    $this->chromosome[$i] = $r->next_int() % self::$MAXLEN;
	}
	$this->chromosome[self::$NBGENE - 1] = $r->next_int() % (self::$MAXLEN + 1);
	//var_dump($this->chromosome);
    }
    

}


// test
/*
$c=array(1,2,3,4,5,6,7,8,9);
$tt = new Genome();
$tt->new_genome($c);
*/
/*
$r = new random();
$t = new genome();
$t->new_ran_genome($r);
*/
?>
